package cn.relx.service.service.impl;

import cn.relx.basic.config.BussinessException;
import cn.relx.basic.util.CodeGenerateUtils;
import cn.relx.basic.util.DistanceUtil;
import cn.relx.basic.util.PageList;
import cn.relx.pet.domain.SearchMasterMsg;
import cn.relx.service.domain.OrderProduct;
import cn.relx.service.domain.Product;
import cn.relx.service.mapper.OrderProductMapper;
import cn.relx.service.mapper.ProductMapper;
import cn.relx.service.query.OrderProductQuery;
import cn.relx.service.service.IOrderProductService;
import cn.relx.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author relx
 * @since 2022-11-03
 */
@Service
public class OrderProductServiceImpl extends BaseServiceImpl<OrderProduct> implements IOrderProductService {
    @Autowired
    private OrderProductMapper orderProductMapper;
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void addOrder(Product product,Long shopId,Long userId) {
        OrderProduct orderProduct =  new OrderProduct();
        orderProduct.setDigest("[服务订单]购买:" + product.getName());
        orderProduct.setOrderSn(CodeGenerateUtils.generateUnionPaySn()+product.getId());
        orderProduct.setPaySn(CodeGenerateUtils.generateUnionPaySn());
        orderProduct.setProductId(product.getId());
        orderProduct.setShopId(shopId);
        orderProduct.setUserId(userId);
        orderProduct.setPrice(product.getSaleprice());
        orderProduct.setState(1);
        orderProductMapper.add(orderProduct);
    }

    @Override
    public PageList<OrderProduct> findOrderProduct(OrderProductQuery query) {
        if(query.getKeyword().equals("已下单")){
            query.setKeyword("");
            query.setState(1);
        }
        if (query.getKeyword().equals("订单取消")){
            query.setKeyword("");
            query.setState(-1);
        }
        if (query.getKeyword().equals("已完成")){
            query.setKeyword("");
            query.setState(2);
        }
        if (query.getKeyword().equals("订单异常")){
            query.setKeyword("");
            query.setState(0);
        }
        if (query.getKeyword().equals("已接单")){
            query.setKeyword("");
            query.setState(4);
        }
        return getOrderProductPageList(query);
    }

    /**
     * 消除订单
     * @param id
     */
    @Override
    public void updateEliminate(Long id) {
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setState(3);
        orderProduct.setId(id);
        orderProductMapper.updateEliminate(orderProduct);
    }

    /**
     * 已接单
     * @param id
     */
    @Override
    public void ordersReceived(Long id) {
        OrderProduct orderProduct = new OrderProduct();
        orderProduct.setState(4);
        orderProduct.setId(id);
        orderProductMapper.updateEliminate(orderProduct);
    }

    private PageList<OrderProduct> getOrderProductPageList(OrderProductQuery query) {
        Integer totals = orderProductMapper.queryBySerchMasterMsgCount(query);
        if (null == totals || 0 == totals){
            return new PageList<>(0,new ArrayList<>());
        }
        // 查询数据
        List<OrderProduct> ts = orderProductMapper.findOrderProduct(query);

        //方到返回里面
        return new PageList<>(totals,ts);
    }
}
